package megajournal.DAO.query;

import java.util.List;

import megajournal.DAO.HibernateUtil;
import megajournal.domain.*;
import org.hibernate.Session;
import org.hibernate.Hibernate;

/**
 *
 * @author cska
 *
 */

public class Select {

    private Session session;

    public Select() {
        super();
    }

    private void begin() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
    }

    private void commit() {
        session.getTransaction().commit();
    }

    public List<Group> groups() {
        begin();
        List<Group> groups = session.createQuery("from megajournal.domain.Group g order by g.letter, g.course, g.number").list();
        for (Group group : groups)
            Hibernate.initialize(group.getStudents());
        commit();

        return groups;
    }

    public Group group(Long id) {
        begin();
        Group group = (Group) session.load(Group.class, id);
        Hibernate.initialize(group.getStudents());
        commit();

        return group;
    }

    public List<Teacher> teachers() {
        begin();
        List<Teacher> teachers = session.createQuery("from megajournal.domain.Teacher t order by t.rank, t.fio").list();
        commit();

        return teachers;
    }

    public Teacher teacher(Long id) {
        begin();
        Teacher teacher = (Teacher) session.load(Teacher.class, id);
        commit();

        return teacher;
    }

    public List<Subject> subjects() {
        begin();
        List<Subject> subjects = session.createQuery("from megajournal.domain.Subject s order by s.name").list();
        commit();

        return subjects;
    }

    public Subject subject(Long id) {
        begin();
        Subject subject = (Subject) session.load(Subject.class, id);
        commit();

        return subject;
    }

    public List<Student> students() {
        begin();
        List<Student> students = session.createQuery("from megajournal.domain.Student s order by s.fio").list();
        for (Student student : students) {
            Hibernate.initialize(student.getGroup());
            Hibernate.initialize(student.getSkips());
            Hibernate.initialize(student.getLs());
        }
        commit();

        return students;
    }

    public Student student(Long id) {
        begin();
        Student student = (Student) session.load(Student.class, id);
        Hibernate.initialize(student.getGroup());
        Hibernate.initialize(student.getSkips());
        Hibernate.initialize(student.getLs());
        commit();

        return student;
    }

    public List<Lesson> lessons() {
        begin();
        List<Lesson> lessons = session.createQuery("from megajournal.domain.Lesson l order by l.date, l.type, l.theme, l.plan").list();
        for (Lesson lesson : lessons) {
            Hibernate.initialize(lesson.getSubject());
            Hibernate.initialize(lesson.getGroup());
            Hibernate.initialize(lesson.getSkips());
        }
        commit();

        return lessons;
    }

    public Lesson lesson(Long id) {
        begin();
        Lesson lesson = (Lesson) session.load(Lesson.class, id);
        Hibernate.initialize(lesson.getSubject());
        Hibernate.initialize(lesson.getGroup());
        Hibernate.initialize(lesson.getSkips());
        commit();

        return lesson;
    }

    public List<Skip> skips() {
        begin();
        List<Skip> skips = session.createQuery("from megajournal.domain.Skip s order by s.student.fio, s.cause").list();
        for (Skip skip : skips) {
            Hibernate.initialize(skip.getLesson());
            Hibernate.initialize(skip.getStudent());
        }
        commit();

        return skips;
    }

    public Skip skip(Long id) {
        begin();
        Skip skip = (Skip) session.load(Skip.class, id);
        Hibernate.initialize(skip.getLesson());
        Hibernate.initialize(skip.getStudent());
        commit();

        return skip;
    }

    public List<Group_Teacher_Subject> gtses() {
        begin();
        List<Group_Teacher_Subject> gtses = session.createQuery("from megajournal.domain.Group_Teacher_Subject g order by g.group.letter, g.group.course, g.group.number, g.teacher.rank, g.teacher.fio, g.subject.name").list();
        for (Group_Teacher_Subject gts : gtses) {
            Hibernate.initialize(gts.getGroup());
            Hibernate.initialize(gts.getTeacher());
            Hibernate.initialize(gts.getSubject());
            Hibernate.initialize(gts.getGroup().getStudents());
        }
        commit();

        return gtses;
    }

    public Group_Teacher_Subject gts(Long id) {
        begin();
        Group_Teacher_Subject gts = (Group_Teacher_Subject) session.load(Group_Teacher_Subject.class, id);
        Hibernate.initialize(gts.getGroup());
        Hibernate.initialize(gts.getTeacher());
        Hibernate.initialize(gts.getSubject());
        commit();

        return gts;
    }

    public List<Lab> labs() {
        begin();
        List<Lab> labs = session.createQuery("from megajournal.domain.Lab l order by l.begin_date, l.text").list();
        for (Lab lab : labs) {
            Hibernate.initialize(lab.getSubject());
            Hibernate.initialize(lab.getLs());
        }
        commit();

        return labs;
    }

    public Lab lab(Long id) {
        begin();
        Lab lab = (Lab) session.load(Lab.class, id);
        commit();

        return lab;
    }

    public List<Lab_Student> lses() {
        begin();
        List<Lab_Student> lses = session.createQuery("from megajournal.domain.Lab_Student").list();
        for (Lab_Student ls : lses) {
            Hibernate.initialize(ls.getStudent());
            Hibernate.initialize(ls.getLab());
            Hibernate.initialize(ls.getInterims());
        }
        commit();

        return lses;
    }

    public Lab_Student ls(Long id) {
        begin();
        Lab_Student ls = (Lab_Student) session.load(Lab_Student.class, id);
        Hibernate.initialize(ls.getStudent());
        Hibernate.initialize(ls.getLab());
        Hibernate.initialize(ls.getInterims());
        commit();

        return ls;
    }

    public List<Interim> interims() {
        begin();
        List<Interim> interims = session.createQuery("from megajournal.domain.Interim").list();
        commit();

        return interims;
    }

    public Interim interim(Long id) {
        begin();
        Interim interim = (Interim) session.load(Interim.class, id);
        commit();

        return interim;
    }

    public List<Admin> admins() {
        begin();
        List<Admin> admins = session.createQuery("from megajournal.domain.Admin").list();
        commit();

        return admins;
    }

    public Admin admin(Long id) {
        begin();
        Admin admin = (Admin) session.load(Admin.class, id);
        commit();

        return admin;
    }

}
